package com.xudow.app.dynamicstate_old.module.follow;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jakewharton.rxbinding.view.RxView;
import com.jude.beam.bijection.RequiresPresenter;
import com.jude.beam.expansion.list.BeamListFragment;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.decoration.DividerDecoration;
import com.jude.utils.JUtils;
import com.umeng.message.PushAgent;
import com.umeng.message.UmengMessageHandler;
import com.umeng.message.entity.UMessage;
import com.xudow.app.R;
import com.xudow.app.XApplication;
import com.xudow.app.dynamicstate_old.domain.Config;
import com.xudow.app.dynamicstate_old.domain.MessageEvent;
import com.xudow.app.dynamicstate_old.domain.entity.Dynamic;
import com.xudow.app.dynamicstate_old.module.follow.viewholder.DynamicContentViewHolder;
import com.xudow.app.dynamicstate_old.module.follow.viewholder.PostLongViewHolder;
import com.xudow.app.dynamicstate_old.module.follow.viewholder.PostNormalViewHolder;
import com.xudow.app.dynamicstate_old.module.follow.viewholder.RepostViewHolder;
import com.xudow.app.newui.PreLoginActivity;

import org.greenrobot.eventbus.EventBus;

import static com.xudow.app.dynamicstate_old.module.DynamicStateFragment.EVENT_GO_TO_DISCOVER;

/**
 * Created by ske on 2016/6/10.
 * 关注模块
 */
@RequiresPresenter(FollowPresenter.class)
public class FollowFragment extends BeamListFragment<FollowPresenter, Dynamic> {
    private static final int POST_NORMAL = 1;
    private static final int POST_LONG = 2;
    private static final int REPOST = 3;
//    @BindView(R.id.text_msg)
//    TextView textMsg;
//    @BindView(R.id.ll_new_msg)
//    LinearLayout llNewMsg;


    @Override
    public BaseViewHolder getViewHolder(ViewGroup parent, int viewType) {

        DynamicContentViewHolder viewHolder = null;
        if (viewType == POST_LONG) {
            viewHolder = new PostLongViewHolder(getPresenter().getAdapter(), parent);
        } else if (viewType == POST_NORMAL) {
            viewHolder = new PostNormalViewHolder(getPresenter().getAdapter(), parent);
        } else if (viewType == REPOST) {
            viewHolder = new RepostViewHolder(getPresenter().getAdapter(), parent);
        } else {
            viewHolder = new PostNormalViewHolder(getPresenter().getAdapter(), parent);
        }
            return viewHolder;

    }

    XApplication app;

//  Unbinder unbinder;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
//        unbinder = ButterKnife.bind(this, view);
        setListener();
        return view;
    }

    private void setListener() {
//        llNewMsg.setOnClickListener(v -> {
//            llNewMsg.setVisibility(View.GONE);
//            JUtils.getSharedPreference().edit().remove("msg_num").apply();
//            startActivity(new Intent(getActivity(), MsgListActivity.class));
//        });
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        app = (XApplication) getActivity().getApplication();
        try{

            getListView().setEmptyView(getEmptyView());
            getListView().setErrorView(getEmptyView());

            getListView().addItemDecoration(new DividerDecoration(getResources().getColor(R.color.gray100), JUtils.dip2px(12)));

        }catch (Exception e){

        }
//        registerUmengMessage();
    }

//    private void registerUmengMessage() {
//        PushAgent.getInstance(getActivity()).setMessageHandler(new UmengMessageHandler() {
//            @Override
//            public void dealWithNotificationMessage(Context context, UMessage uMessage) {
//                int localNum = JUtils.getSharedPreference().getInt("msg_num", 0);
//                textMsg.setText(localNum + 1 + "条消息");
//                JUtils.getSharedPreference().edit().putInt("msg_num", ++localNum).apply();
//                llNewMsg.setVisibility(View.VISIBLE);
//                super.dealWithNotificationMessage(context, uMessage);
//            }
//            @Override
//            public void dealWithCustomMessage(Context context, UMessage uMessage) {
//                super.dealWithCustomMessage(context, uMessage);
//            }
//        });
//    }

//    @Override
//    public int getLayout() {
//        return R.layout.fragment_follow;
//    }

    @Override
    public int getViewType(int position) {
        try{
        Dynamic dynamic = getPresenter().getAdapter().getItem(position);
        // 1：长文，2：普通
        int type = dynamic.getType();
        // 上级动态主键, 为0说明，这条动态不是转发的
        long pId = dynamic.getpId();
        if (pId != 0)
            return REPOST;
        else if (type == 1)
            return POST_LONG;
        else if (type == 2)
            return POST_NORMAL;
    }catch (Exception e){
        Log.i("FollowFragment","getViewType"+e);
    }
        return 0;
    }

    int clickPos;

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        try{
        if (resultCode == Activity.RESULT_OK) {
            getPresenter().onRefresh();
        }
    }catch (Exception e){
        Log.i("FollowFragment","onActivityResult"+e);
    }
    }

    View getEmptyView() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.discover_view_empty, null, false);
        RxView.clicks(view.findViewById(R.id.tag_go_discover))
                .subscribe(v -> {
                    EventBus.getDefault().post(new MessageEvent(EVENT_GO_TO_DISCOVER));
                });
        RxView.clicks(view.findViewById(R.id.tag_create_dynamic))
                .subscribe(v -> {
                    if (XApplication.getInstance().isLogin())
                        startActivityForResult(new Intent(getContext(), NewDynamicActivity.class), Config.Code_NeedRefresh);
                    else
                        startActivity(new Intent(getContext(), PreLoginActivity.class));
                });
        return view;
    }


}
